package GUI.AddBooking;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import GUI.CustomerScreen;
import GUI.Controller.Controller;

@SuppressWarnings("serial")
public class CustomerViewAvailableRoomsPanel extends JPanel{
	private CustomerScreen customer_screen;
	private JTable bookingTable;

	private JPanel north_panel;
	private JScrollPane scrollPane;

	private JButton book;

	private JButton cancel;

	String[] columnNames ={
			"Room ID", "Room #", "Room Type", "Hotel Name", "Room Cost"};

	public CustomerViewAvailableRoomsPanel(){
		customer_screen = CustomerScreen.getInstance();
		this.setLayout(new BorderLayout());
		this.setVisible(true);
		SetUpNorthPanel();
		SetUpTable();
	}

	private void SetUpNorthPanel(){

		north_panel = new JPanel();
		north_panel.setLayout(new BorderLayout());
		this.add(north_panel, BorderLayout.NORTH);

		book = new JButton("Book Room");
		north_panel.add(book, BorderLayout.WEST);
		book.addActionListener(new BookRoomButton());

		cancel = new JButton("Cancel");
		north_panel.add(cancel, BorderLayout.EAST);
		cancel.addActionListener(new CancelButton());
	}

	private void SetUpTable(){
		Object[][] tableData;
		try{
			tableData= Controller.GetAvailableRooms(customer_screen.getAvailableRoomsInfo());
		}catch(NullPointerException e){
			tableData = null;
		}

		bookingTable = new JTable(tableData, columnNames);

		bookingTable.setModel(new DefaultTableModel(tableData, columnNames){
			@Override 
			public boolean isCellEditable(int row, int column) { 
				return false; 
			} 
		});
		bookingTable.setPreferredScrollableViewportSize(new Dimension(500, 455));
		bookingTable.setFillsViewportHeight(true);

		if(scrollPane!=null)
			remove(scrollPane);
		scrollPane = new JScrollPane(bookingTable);
		this.add(scrollPane, BorderLayout.SOUTH);
	}

	public void Update(){
		SetUpTable();
		this.setVisible(false);
		this.setVisible(true);
	}

	private class BookRoomButton implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// input[0] -> customerID
			// input[1] -> roomID
			// input[2] -> start_date
			// input[3] -> end_date
			if(bookingTable.getSelectedRow() == -1){
				JOptionPane.showMessageDialog(customer_screen, "Please select a room",
						"Error",
						JOptionPane.WARNING_MESSAGE);
			}
			else{
				String[] query = {null, null, null, null};
				String[] dates = customer_screen.getBookingDates();

				int row_index = bookingTable.getSelectedRow();

				int ID = customer_screen.getID();
				System.out.println(ID);

				query[0] = Integer.toString(ID);
				query[1] = (String) bookingTable.getValueAt(row_index, 0);


				query[2] = dates[0];
				query[3] = dates[1];

				try{
					if(Controller.bookRoom(query)){
						Update();
						JOptionPane.showMessageDialog(customer_screen, "Room Booked!");
						customer_screen.AddBooking();
					}
					else
						JOptionPane.showMessageDialog(customer_screen, "Invalid Room Choice");

				}
				catch(Exception e){
					JOptionPane.showMessageDialog(customer_screen, "Invalid Room Choice");
				}
			}
		}
	}
	
	private class CancelButton implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			customer_screen.AddBooking();
		}
	}
}
